package sd.czw.com.miemiegoushop.activity;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import sd.czw.com.miemiegoushop.R;
import sd.czw.com.miemiegoushop.base.CzwBaseActivity;

/**
 * Created by czw
 * desc 描述：
 * on 2017/9/29.
 */
public class BottomSheetDialogActivity extends CzwBaseActivity {

    @Bind(R.id.btn_dialog)
    Button btnDialog;
    @Bind(R.id.btn_dialog1)
    Button btnDialog1;
    @Bind(R.id.btn_dialog2)
    Button btnDialog2;
    private BottomSheetDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bottomdialog;
    }

    @Override
    public void initView() {
        //底部列表分享dialog
        getBottomDialog();
        //recycleview底部dialog
        getJiuGongGeDialog();
        //九宫格底部dialog
        getJGGdialog();


        btnDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog.isShowing()){
                    dialog.dismiss();
                }else {
                    dialog.show();
                }
            }
        });

    }

    private void getJGGdialog() {
        final BottomSheetDialog dialog2 = new BottomSheetDialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.bottomdialog_rv,null);
        getGrideView(view);
        dialog2.setContentView(view);
        dialog2.setCancelable(true);
        dialog2.setCanceledOnTouchOutside(true);

        // 解决下滑隐藏dialog 后，再次调用show 方法显示时，不能弹出Dialog
        View view2 = dialog2.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet);
        final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(view2);
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    dialog2.dismiss();
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });

        btnDialog2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog2.isShowing()){
                    dialog2.dismiss();
                }else {
                    dialog2.show();
                }
            }
        });
    }

    private void getGrideView(View view) {
        GridView gridView = (GridView) view.findViewById(R.id.gv);
        gridView.setAdapter(new MyGridAdapter(BottomSheetDialogActivity.this,mockGvData()));

    }

    class MyGridAdapter extends BaseAdapter{

        private Context context;
        private List<GvModel> gvdata;

        public MyGridAdapter(Context context,List<GvModel> gvdata) {
            this.context = context;
            this.gvdata = gvdata;
        }

        @Override
        public int getCount() {
            return gvdata.size();
        }

        @Override
        public Object getItem(int i) {
            return gvdata.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            MyVieHolder myViewHolder;
            if (view == null){
                view = LayoutInflater.from(context).inflate(R.layout.gv_item,null);
                myViewHolder = new MyVieHolder();
                myViewHolder.imageView = (ImageView) view.findViewById(R.id.gv_iv);
                myViewHolder.textView = (TextView) view.findViewById(R.id.gv_tv);
                view.setTag(myViewHolder);
            }else {
                myViewHolder = (MyVieHolder) view.getTag();
            }

            Glide.with(context).load(gvdata.get(i).getPath()).into(myViewHolder.imageView);
            myViewHolder.textView.setText(gvdata.get(i).getTitle());
            return view;
        }


        class MyVieHolder{
            ImageView imageView;
            TextView textView;
        }
    }

    /**
     * recycleview底部dialog
     */
    private void getJiuGongGeDialog() {
        final BottomSheetDialog dialog1 = new BottomSheetDialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_dialog,null);

        handleList(view);

        dialog1.setContentView(view);
        dialog1.setCancelable(true);
        dialog1.setCanceledOnTouchOutside(true);
        // 解决下滑隐藏dialog 后，再次调用show 方法显示时，不能弹出Dialog
        View view1 = dialog1.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet);
        final BottomSheetBehavior bottomSheetBehavior1 = BottomSheetBehavior.from(view1);
        bottomSheetBehavior1.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    dialog1.dismiss();
                    bottomSheetBehavior1.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });
        btnDialog1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog1.isShowing()){
                    dialog1.dismiss();
                }else {
                    dialog1.show();
                }
            }
        });

    }

    private void handleList(View contentView){
        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerView);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        MusicAdapter adapter = new MusicAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setData(mockData());
        adapter.notifyDataSetChanged();
    }

    /**
     * 底部列表分享dialog
     */
    private void getBottomDialog() {
        dialog=new BottomSheetDialog(this);
        View dialogView= LayoutInflater.from(this).inflate(R.layout.bottomdialog_item,null,false);
        dialog.setContentView(dialogView);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        // 解决下滑隐藏dialog 后，再次调用show 方法显示时，不能弹出Dialog
        View view1 = dialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet);
        final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(view1);
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    dialog.dismiss();
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });
    }

    @Override
    public void initData() {

    }

    private List<GvModel> mockGvData(){
        List<GvModel> gvdata = new ArrayList<>();
        gvdata.add(new GvModel("http://img01.sogoucdn.com/app/a/100520024/dd4ec94c270a74353959ae4f976614ef","九宫格一"));
        gvdata.add(new GvModel("http://img04.sogoucdn.com/app/a/100520024/a453c4d03df29f3e952ce445a407aaf4","九宫格一"));
        gvdata.add(new GvModel("http://img04.sogoucdn.com/app/a/100520024/68f46a805ae8547428a149a3f6e0eeeb","九宫格一"));
        gvdata.add(new GvModel("http://img01.sogoucdn.com/app/a/100520024/dd4ec94c270a74353959ae4f976614ef","九宫格一"));
        gvdata.add(new GvModel("http://img04.sogoucdn.com/app/a/100520024/a453c4d03df29f3e952ce445a407aaf4","九宫格一"));
        gvdata.add(new GvModel("http://img01.sogoucdn.com/app/a/100520024/f8661955ac5bff5fab4059309beb3f5e","九宫格一"));
        gvdata.add(new GvModel("http://img04.sogoucdn.com/app/a/100520024/a453c4d03df29f3e952ce445a407aaf4","其他"));
        return gvdata;
    }

    /**
     * 模拟数据
     * @return
     */
    private List<MusicInfo> mockData(){
        List<MusicInfo> musicInfos = new ArrayList<>();
        MusicInfo musicInfo = new MusicInfo("哪里都是你","周杰伦");
        musicInfos.add(musicInfo);
        MusicInfo musicInfo1 = new MusicInfo("阳光宅男","周杰伦");
        musicInfos.add(musicInfo1);
        MusicInfo musicInfo2 = new MusicInfo("可爱女人","周杰伦");
        musicInfos.add(musicInfo2);
        MusicInfo musicInfo3 = new MusicInfo("火车叨位去","周杰伦");
        musicInfos.add(musicInfo3);
        MusicInfo musicInfo4 = new MusicInfo("我的地盘","周杰伦");
        musicInfos.add(musicInfo4);
        MusicInfo musicInfo5 = new MusicInfo("枫","周杰伦");
        musicInfos.add(musicInfo5);
        MusicInfo musicInfo6 = new MusicInfo("搁浅","周杰伦");
        musicInfos.add(musicInfo6);
        MusicInfo musicInfo7 = new MusicInfo("一路向北","周杰伦");
        musicInfos.add(musicInfo7);
        MusicInfo musicInfo8 = new MusicInfo("半岛铁盒","周杰伦");
        musicInfos.add(musicInfo8);
        MusicInfo musicInfo9 = new MusicInfo("霍元甲","周杰伦");
        musicInfos.add(musicInfo9);

        return musicInfos;
    }


    public static class MusicAdapter extends RecyclerView.Adapter{
        private List<MusicInfo> mData;

        public void setData(List<MusicInfo> data) {
            mData = data;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MusicViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.bottom_sheet_music_item,null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            MusicViewHolder musicViewHolder = (MusicViewHolder) holder;

            MusicInfo musicInfo = mData.get(position);

            musicViewHolder.name.setText(musicInfo.name);

            musicViewHolder.singer.setText(musicInfo.singer);
        }

        @Override
        public int getItemCount() {
            return mData == null ? 0:mData.size();
        }

        public static class MusicViewHolder extends RecyclerView.ViewHolder{
            public TextView name;
            public TextView singer;
            public MusicViewHolder(View itemView) {
                super(itemView);
                name = (TextView) itemView.findViewById(R.id.music_name);
                singer = (TextView) itemView.findViewById(R.id.music_singer);
            }
        }
    }

}
